import { Component, OnInit, Input, Output, EventEmitter } from "@angular/core";
import { UserService } from "./../user.service";
import { User } from "./../user.model";
import { SIGNUPSMS } from "./../../shared";

// import toolTip from "../../util/tooltip.js";
interface SignUser {
  mobile: string;
  smsCode: string;
  smsToken?: string;
}

@Component({
  selector: "user-signup",
  templateUrl: "signup.component.html",
  styleUrls: ["signup.component.less"]
})
export class UserSignupComponent implements OnInit {
  @Output() setModal = new EventEmitter();
  errText: string = "";
  waitTime: number = 0;
  signUser: SignUser = {
    mobile: "",
    smsCode: "",
    smsToken: ""
  };
  status: string; //点击状态
  constructor(private userService: UserService) { }

  ngOnInit() {

  }

  signup() {
    if (this.signUser.mobile && this.signUser.smsCode && this.signUser.smsToken) {
      this.errText = "";
      if (!this.checkInfoValid()) {
        return;
      }
      if (!this.smsCodeValidCheck()) {
        return;
      }
      console.log("submit obj:", this.signUser);
      this.errText = "";
      this.userService.signup(this.signUser).subscribe((data) => {
        if ( data.code !== 0 ) {
          this.errText = data.msg;
        }else {
          this.errText = "注册成功";
        }
        // hide sign modal
        // this.userService.setUser(data);
        // console.log("signup success data:", data);
      }, (err) => {
        console.log("signup failed:", err);
        this.errText = "服务连接超时，请稍后重试！";
      });
    }
  }

  // 验证码设定检测
  private smsCodeValidCheck() {
    let val = true;
    this.errText = "";
    if (!this.signUser.smsCode) {
      this.errText = "验证码不正确"; // 验证码不能为空
      val = false;
    }
    else {
      let fmt = /^[\d\w]{6}$/;
      if (!fmt.test(this.signUser.smsCode)) {
        this.errText = "验证码不正确";
        val = false;
      }
    }
    return val;
  }

  private checkInfoValid() {
    let val = true;
    // 校验手机号是否可用
    if (!this.signUser.mobile) {
      this.errText = "手机号码不能为空";
      val = false;
    } else {
      // let fmt = /^\d{11}$/;
      let fmt = /^1[3578]\d{9}$/;
      if (!fmt.test(this.signUser.mobile)) {
        this.errText = "手机号格式不正确";
        val = false;
      }
    }
    return val;
  }

  sendSmsCode() {
    this.errText = "";
    if (!this.checkInfoValid()) {
      return;
    }
    this.userService.sendSmsCode( `${SIGNUPSMS}`, this.signUser.mobile).subscribe((data) => {
      if ( data.code !== 0 ) {
        this.errText = data.msg;
      }
      else {
        this.errText = "";
        this.signUser.smsToken = data.data.smsToken;
        this.waitTime = 60;
        let t = setInterval(() => {
          this.waitTime--;
          if (this.waitTime <= 0) {
            clearInterval(t);
          }
        }, 1000);
      }
    }, (err) => {
      console.log("sms/otps failed data:", err);
      this.errText = "服务连接超时，请稍后重试！";
    });
  }
  closeAgreement(action: string) {
    switch (action) {
      case "hide":
        this.status = "";
        break;
      default:
        break;
    }
  }
  clickAgreement() {
    this.status = "clicked";
  }
  changeModal(action: string) {
    if (action === "hide" && !this.userService.isToken) {
    } else {
      this.errText = "";
      this.setModal.emit(action);
    }
  }
}